//
//  ShareView.m
//  LazyCat
//
//  Created by zhanshu on 15/9/9.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "ShareView.h"

@implementation ShareView
+ (ShareView*)share {
    ShareView *share = [[[NSBundle mainBundle] loadNibNamed:@"ShareView" owner:self options:nil] firstObject];
    share.frame = [UIScreen mainScreen].bounds;
//    share.arr = @[[NSNumber numberWithInteger:ShareTypeWeixiSession],[NSNumber numberWithInteger:ShareTypeWeixiTimeline],[NSNumber numberWithInteger:ShareTypeQQ],[NSNumber numberWithInteger:ShareTypeQQSpace],[NSNumber numberWithInteger:ShareTypeSinaWeibo],[NSNumber numberWithInteger:ShareTypeCopy]];
    return share;
}
- (void)show {
    self.backgroundColor = SetColor(0, 0, 0, 0.5);
    [[UIApplication sharedApplication].keyWindow addSubview:self];

    _backView.frame = CGRectMake(0, self.bottom, ScreenWidth, _backView.height);
    [UIView animateWithDuration:0.35 animations:^{
        _backView.frame = CGRectMake(0, self.bottom-_backView.height, ScreenWidth, _backView.height);
    } completion:^(BOOL finished) {
    }];
}
- (void)showMess:(NSString *)content withImgUrl:(NSString *)imgUrl url:(NSString *)url{
    [self show];
    if ([content rangeOfString:@"http"].location !=NSNotFound) {
        _shareMess = @"我在懒猫社长发现一个非常不错的商品，赶快来看看吧!";
        NSArray *array = [content componentsSeparatedByString:@"http"]; //从字符A中分隔成2个元素的数组
        _shareWZ = [NSString stringWithFormat:@"%@",array[0]];
    }else if ([content rangeOfString:@"999"].location !=NSNotFound) {
        NSArray *array = [content componentsSeparatedByString:@"999"]; //从字符A中分隔成2个元素的数组
        _shareWZ = [NSString stringWithFormat:@"%@",array[0]];;
        _shareMess = [NSString stringWithFormat:@"%@",array[1]];
        
    }else{
        _shareWZ = content;
    }
    _shareImg = [NSString stringWithFormat:@"%@",imgUrl];
    _shareUrl = url;
//    _shareMess = content;
//    _shareImg = imgUrl;
//    _shareUrl = url;
}
#pragma 点击事件
- (IBAction)shareBtnClick:(UIButton *)sender {
    [self dismissView];
    
    switch (sender.tag) {
        case 100:
            [self shareType:ShareTypeWeixiSession];
            break;
        case 101:
            [self shareType:ShareTypeWeixiTimeline];
            break;
        case 102:
            [self shareType:ShareTypeQQ];
            break;
        case 103:
            [self shareType:ShareTypeQQSpace];
            break;
        case 104:
            [self shareType:ShareTypeSinaWeibo];
            break;
        case 105:
            [self shareType:ShareTypeCopy];
            break;

            
        default:
            break;
    }
}
- (IBAction)cancle:(UIButton *)sender {
    [self dismissView];
    
}
//- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//    [self dismissView];
//}
#pragma mark -隐藏视图
- (void)dismissView {
    _backView.frame = CGRectMake(0, self.bottom-_backView.height, ScreenWidth, _backView.height);
    [UIView animateWithDuration:0.35 animations:^{
        _backView.frame = CGRectMake(0, self.bottom, ScreenWidth, _backView.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - 分享
- (void)shareType:(ShareType)type {
//    NSString *imagePath = [[NSBundle mainBundle] pathForResource:IMAGE_NAME ofType:IMAGE_EXT];
    //[NSString stringWithFormat:MyCode,[AppData share].username]
    //构造分享内容
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"logo80" ofType:@"png"];
    NSString *shareContent = [NSString stringWithFormat:@"%@",_shareMess];
    if (type == ShareTypeCopy) {
        shareContent =[NSString stringWithFormat:@"%@",_shareUrl];
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string =[NSString stringWithFormat:@"%@",_shareUrl];
        [[MessShowView share] showWithMess:@"拷贝成功"];
        return;
    }
    
//      [_shareImg containsString:@"http"]?[ShareSDK imageWithUrl:_shareImg]:[ShareSDK imageWithPath:imagePath];
    
    //      [[_shareImg rangeOfString:@"http"].location != NSNotFound]?[ShareSDK imageWithUrl:_shareImg]:[ShareSDK imageWithPath:imagePath];
    
    id imgUrl;
    if ([_shareImg rangeOfString:@"http"].location != NSNotFound) {
      imgUrl =  [ShareSDK imageWithUrl:_shareImg];
  
        
    }else{
      imgUrl =  [ShareSDK imageWithPath:imagePath];
 
    }
    
    id<ISSContent> publishContent = [ShareSDK content:_shareMess
                                       defaultContent:@""
                                                image:imgUrl
                                                title:_shareWZ
                                                  url:_shareUrl
                                          description:_shareMess
                                            mediaType:SSPublishContentMediaTypeNews];
    [ShareSDK showShareViewWithType:type container:nil content:publishContent statusBarTips:NO authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        if (state == SSPublishContentStateSuccess)
        {
            NSLog(NSLocalizedString(@"TEXT_SHARE_SUC", @"分享成功"));
            [[MessShowView share] showWithMess:@"分享成功"];
        }
        else if (state == SSPublishContentStateFail)
        {
            NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
            [[MessShowView share] showWithMess:[error errorDescription]];
        }
    }];
}


@end
